package 二叉树;

/**
 * TODO
 *
 * @Description
 * @Author Lx
 * @Date 2024/7/3 9:48
 **/
public class 最大二叉树 {
    public static void main(String[] args) {
        int[] nums={3,2,1};
        TreeNode root=constructMaximumBinaryTree(nums);

        PrintBinaryTree.travelTree(root);
//        PrintBinaryTree.treePrint(root);
//        PrintBinaryTree.printTree(root);
    }

    static public TreeNode constructMaximumBinaryTree(int[] nums) {
        if(nums.length==0)return null;

        return solve(nums,0,nums.length);
    }

    static public TreeNode solve(int[] nums,int begin,int end/*左闭右开*/){

        //1.确定最大值及其下标
        if(begin>=end)return null;
        int index=begin;
        int maxval=nums[begin];
        for(int i=begin;i<end;i++){
            if(nums[i]>maxval){
                maxval=nums[i];
                index=i;
            }
        }

        //2.构建root节点
        TreeNode root=new TreeNode(maxval);

        root.left=solve(nums,begin,index);
        root.right=solve(nums,index+1,end);

        return root;
    }
}
